suite("SystemJS Standard Tests", function () {
    test("Syntax errors", function () {
        // mocha must ignore script errors as uncaught
        window.onerror = undefined;
        return System.import("fixtures/error-loader.js").then(
            function () {
                assert.fail("Should fail");
            },
            function (e) {
                assert.ok(e);
                assert.ok(e instanceof ReferenceError);
            }
        );
    });

    test("Fetch errors", function () {
        return System.import("fixtures/error-loader2.js").then(
            function () {
                assert.fail("Should fail");
            },
            function (e) {
                assert.ok(e);
                console.log(e);
                assert.ok(e.message.indexOf("non-existent") !== -1);
                assert.ok(e.message.indexOf("error-loader2.js") !== -1);
            }
        );
    });

    test("String encoding", function () {
        return System.import("fixtures/string-encoding.js").then(function (m) {
            assert.equal(m.pi, decodeURI("%CF%80"));
            assert.equal(m.emoji, decodeURI("%F0%9F%90%B6"));
        });
    });

    test("Import full URL", function () {
        return System.import(
            window.location.href.substr(
                0,
                window.location.href.lastIndexOf("/")
            ) + "/fixtures/browser/string-encoding.js"
        ).then(function () {
            assert.ok(true);
        });
    });

    test("Package maps", function () {
        [
            ["a", rootURL + "b"],
            ["f", "a:"],
            /* 这里因为偏移原因导致位置不对 */
            ["a/b", baseURL + "test/fixtures/browser/a/b"],
            ["b/c", rootURL + "d/c"],
            ["b.js", "http://jquery.com/jquery.js"],
            ["b.js/c", "http://jquery.com/c"],
            ["g/x", "https://site.com/x"],
        ].map(([i0, i1]) => {
            assert.equal(System.resolve(i0), i1);
        });
    });

    test("import.meta.resolve package maps", function () {
        return System.import("fixtures/resolve.js")
            .then(function (m) {
                return m.resolve("a");
            })
            .then(function (resolved) {
                assert.equal(resolved, rootURL + "b");
            });
    });

    test("import.meta.resolve package maps paths", function () {
        return System.import("fixtures/resolve.js")
            .then(function (m) {
                return m.resolve("a/");
            })
            .then(function (resolved) {
                assert.equal(resolved, baseURL + "test/fixtures/browser/a/");
            });
    });

    test("Contextual package maps", function () {
        return System.import("fixtures/scope-test/index.js").then(function (m) {
            assert.equal(m.mapdep, "mapdep");
        });
    });

    test("import.meta.resolve contextual package maps", function () {
        return System.import("fixtures/resolve.js")
            .then(function (m) {
                return m.resolve(
                    "maptest",
                    baseURL + "test/fixtures/browser/scope-test/index.js"
                );
            })
            .then(function (resolved) {
                assert.equal(
                    resolved,
                    baseURL + "test/fixtures/browser/contextual-map-dep.js"
                );
            });
    });

    test("import.meta.resolve contextual package maps fail", function () {
        return System.import("fixtures/resolve.js")
            .then(function (m) {
                return m.resolve("maptest");
            })
            .then(
                function (resolved) {
                    assert.ok(false);
                },
                function (error) {
                    assert.equal(error.message.indexOf("Unable to resolve"), 0);
                }
            );
    });

    test("Loading named System.register fails", function () {
        return System.import("fixtures/named-register.js")
            .then(function () {
                assert.fail("Should fail");
            })
            .catch(function (err) {
                assert.ok(err);
            });
    });

    // test("Global script loading", function () {
    //     return System.import("fixtures/global.js").then(function (m) {
    //         assert.ok(m.default);
    //         assert.equal(m.default.some, "thing");
    //     });
    // });

    // test("firstGlobalProp option", function () {
    //     Object.getPrototypeOf(System).firstGlobalProp = true;
    //     return System.import("fixtures/multiple-globals.js").then(function (m) {
    //         delete Object.getPrototypeOf(System).firstGlobalProp;
    //         assert.ok(m.default);
    //         assert.equal(m.default.foo1, "foo1");
    //     });
    // });

    // test("Parallel Global loading", function () {
    //     var scriptsToLoad = [];
    //     for (var i = 1; i < 11; i++)
    //         scriptsToLoad.push("fixtures/globals/import" + i + ".js");

    //     return Promise.all(
    //         scriptsToLoad.map(function (s, index) {
    //             return System.import(s).then(function (m) {
    //                 assert.equal(m.default, index + 1);
    //             });
    //         })
    //     );
    // });

    // test("Catches global script errors", function () {
    //     // mocha must ignore script errors as uncaught
    //     window.onerror = undefined;
    //     return System.import("fixtures/eval-err.js").then(
    //         function () {
    //             assert.fail("Should fail");
    //         },
    //         function (e) {
    //             assert.ok(e);
    //             assert.equal(e.message, "Global eval error");
    //         }
    //     );
    // });

    test("Contextual dynamic import", function () {
        return System.import("fixtures/dynamic-import-register.js")
            .then(function (m) {
                return m.lazy();
            })
            .then(function (lazyValue) {
                assert.equal(lazyValue, 5);
            });
    });

    test("Errors for bad Content-Type headers", function () {
        return System.import("fixtures/content-type-none.json")
            .catch(function (err) {
                assert.ok(/Unknown Content-Type.*error#4/i.test(err));
            })
            .then(function () {
                return System.import("fixtures/content-type-xml.json");
            })
            .catch(function (err) {
                assert.ok(/Unknown Content-Type.*error#4/i.test(err));
            });
    });

    // if (typeof Worker !== "undefined")
    test("Using SystemJS in a  Web Classic Worker", function () {
        const worker = new Worker("./browser/worker.js");

        return new Promise(function (resolve) {
            worker.onmessage = function (e) {
                assert.equal(e.data.p, "p");
                resolve();
            };
        });
    });
    test("Using SystemJS in a  Web Module Worker", function () {
        const worker = new Worker("./browser/worker_module.js", {
            type: "module",
        });

        return new Promise(function (resolve) {
            worker.onmessage = function (e) {
                assert.equal(e.data.p, "p");
                resolve();
            };
        });
    });

    test("should load auto import", function () {
        const resolved = System.resolve(
            "./test/fixtures/browser/auto-import.js"
        );
        assert.ok(System.has(resolved));
        assert.equal(System.get(resolved).auto, "import");
    });

    test("import.meta.resolve", function () {
        return System.import("fixtures/resolve.js")
            .then(function (m) {
                return m.resolve("./test.js");
            })
            .then(function (resolved) {
                assert.equal(
                    resolved,
                    baseURL + "test/fixtures/browser/test.js"
                );
            });
    });

    test("non-enumerable __esModule property export (issue 2090)", function () {
        return System.import("fixtures/__esModule.js").then(function (m) {
            // Even though __esModule is not enumerable on the exported object, it should be preserved on the systemjs namespace
            assert.ok(m.__esModule);
        });
    });

    test("should support depcache", function () {
        return System.import("./test/fixtures/browser/depcache.js").then(
            function (m) {
                assert.ok(m);
                assert.equal(m.default, "10th module");
            }
        );
    });

    // https://github.com/systemjs/systemjs/issues/2286
    test("should allow deletion of modules that failed to instantiate", function () {
        return System.import("fixtures/link-error.js").then(
            function () {
                throw Error("Link error expected");
            },
            function () {
                assert.ok(
                    System.delete(System.resolve("fixtures/link-error.js"))
                );
                assert.ok(
                    System.delete(
                        System.resolve("fixtures/link-error-child.js")
                    )
                );
                assert.ok(
                    System.delete(System.resolve("fixtures/not-found.js"))
                );
                assert.ok(
                    System.delete(
                        System.resolve("fixtures/link-error-child2.js")
                    )
                );
            }
        );
    });

    var isIE11 =
        typeof navigator !== "undefined" &&
        navigator.userAgent.indexOf("Trident") !== -1;
});

// suite("module-type", () => {
// function isCSSStyleSheet(obj) {
//     if (isIE11) {
//         return obj.cssRules;
//     } else {
//         return obj instanceof CSSStyleSheet;
//     }
// }
//     test("JSON modules", function () {
//         return System.import("fixtures/json.json")
//             .then(function (m) {
//                 assert.equal(m.default.json, "module");
//             })
//             .then(function () {
//                 return System.import("fixtures/json-error.json");
//             })
//             .catch(function (err) {
//                 assert.ok(err instanceof SyntaxError);
//             })
//             .then(function () {
//                 return System.import("fixtures/json-error.json");
//             })
//             .catch(function (err) {
//                 assert.ok(err instanceof SyntaxError);
//             });
//     });
//     if (typeof WebAssembly !== "undefined" && typeof process === "undefined")
//         test("Loading WASM", function () {
//             return System.import("fixtures/wasm/example.wasm").then(function (
//                 m
//             ) {
//                 assert.equal(m.exampleExport(1), 2);
//             });
//         });

//     if (typeof WebAssembly !== "undefined" && typeof process === "undefined")
//         test("Loading WASM over 4KB limit", function () {
//             return System.import("fixtures/wasm/addbloated.wasm").then(
//                 function (m) {
//                     assert.equal(m.addTwo(1, 1), 2);
//                 }
//             );
//         });

//     test("should load a css module", function () {
//         return System.import("fixturesbase/css-modules/a.css").then(function (
//             m
//         ) {
//             assert.ok(m);
//             assert.ok(isCSSStyleSheet(m.default));
//             document.adoptedStyleSheets = document.adoptedStyleSheets.concat(
//                 m.default
//             );
//         });
//     });
// test("should not get confused by filenames in search params hash when resolving module type", function () {
//   return System.import(
//       "fixturesbase/css-modules/search-param.css?param=foo.html"
//   ).then(function (m) {
//       assert.ok(m);
//       assert.ok(isCSSStyleSheet(m.default));
//   });
// });

//     test("should support application/javascript css module override", function () {
//         return System.import("fixturesbase/css-modules/javascript.css").then(
//             function (m) {
//                 assert.ok(m);
//                 assert.ok(m.css, "module");
//             }
//         );
//     });

//     test("should throw when trying to load an HTML module", function () {
//         return System.import("./test.html").then(
//             function () {
//                 throw Error(
//                     "Loading html modules isn't implemented, but attempting to do so didn't throw an Error"
//                 );
//             },
//             function (err) {
//                 assert.ok(err);
//             }
//         );
//     });
// });
